var app = new Vue({
    el:"#family",
    data () {
        return {
            headerTitle: '家庭服务师合作协议',
            idcardFront: '', // 身份证正面预览
            idcardBack: '', // 身份证背面预览
            housingEstate: '请选择您的地址', // 小区地址
            address: '', // 详细地址
            fullAddress: '',
            params: {
                realName: '', // 姓名
                mobile: '', // 手机号
                areaCode: '', // 小区areaCode
                address: '', // 详细地址
                idcardFrontFile: '', // 身份证正面文件
                idcardBackFile: '' // 身份证背面文件
            },
            showType: 0,
            btnDisabled: false, // 提交按钮是否可用
            layerShow: false, // 地址选择弹窗
            curIndex: 0,
            regions: [ // 区域列表数据
                {
                    id: 0,
                    name: '请选择',
                    list: []
                },
                {
                    id: 0,
                    name: '',
                    list: []
                },
                {
                    id: 0,
                    name: '',
                    list: []
                },
                {
                    id: 0,
                    name: '',
                    list: []
                },
                {
                    id: 0,
                    name: '',
                    list: []
                },
                {
                    id: 0,
                    name: '',
                    list: []
                }
            ],
            curList: [], // 区域当前列表数据
            showAdd: false,
            options: {
                pullUpLoad: false
            },
            items: [
                {
                    text: '1. 家庭服务师为零米生活的个人代理服务人员，以推广的顾客或者会员在去哪买家庭生活服务平台的订单量和销售服务金额获得相应的提成和奖励。'
                },
                {
                    text: '2. 家庭服务师的工作内容为：'
                },
                {
                    text: '（1）帮助不会使用智能手机或者不是会员的顾客在去哪买家庭生活服务平台上下单；'
                },
                {
                    text: '（2）推广、开办去哪买家庭服务平台会员，帮助会员了解和使用去哪买的相关服务；'
                },
                {
                    text: '（3）家庭服务师可以通过线下和在线的方式推广去哪买，不限制推广区域和范围。'
                },
                {
                    text: '3. 家庭服务师可以自己招募下级家庭服务师，组建家庭服务师团队，让团队成员一起更快、更好的推广去哪买家庭生活服务平台，同时获得管理和领导收益。'
                },
                {
                    text: '4. 家庭服务师团队由零米生活科技（深圳）股份有限公司（下称“公司”）市场部统一管理，根据家庭服务师家庭住址归属所在区域市场部团队管辖。'
                },
                {
                    text: '5. 家庭服务师团队的各项培训和支持由负责管辖的市场部门提供。'
                }
            ]
            
        }
    },
    watch: {
        curIndex (val) {
            this.curList = this.regions[val].list
        },
        regions: {
            deep: true,
            handler (val) {
                this.curList = val[this.curIndex].list
            }
        }
    },
    created() {
        let url = window.location.href
        let accessToken = null
        if (url.indexOf('accessToken') !== -1) {
            let params = getUrlParams()
            accessToken = params.accessToken
            let domain = ''
            if (window.location.href.indexOf('laykj.cn')>-1) {
                domain = '.laykj.cn'
            } else {
                domain = document.domain
            }
            setCookie('accessToken', accessToken, 7,domain)
        }
        this.getUserInfo()
    },
    mounted() {
        /* const agreementBox = this.$refs.agreementBox
        agreementBox.show() */
        this.$nextTick(() => {
            this.$refs.agreementScroll.refresh()
        })
    },
    methods: {
        // 返回
        goback () {
            if (this.showType === 1) {
                this.showType = 0
                this.headerTitle = '家庭服务师合作协议'
            } else {
                window.location.href = 'member_info_authentication.html'
            }
        },
        getUserInfo() {
            Axios.post('user/user/userInfo.do',{}).then(res => {})
        },
        btnOk () {
            const agreementBox = this.$refs.agreementBox
            agreementBox.hide()
            this.toast = this.$createToast({
                txt: '申请成为家庭服务师成功，请等待后台系统审核!',
                type: 'txt'
            })
            this.toast.show()
            setTimeout(() => {
                window.location.href = '../../index.html'
            }, 2000)
        },
        btnAgree () {
            this.showType = 1
            this.headerTitle = '申请家庭服务师'
        },
        // 搜索小区弹窗显示
        showAddress () {
            this.showAdd = true
        },
        changeShow (data) {
            this.showAdd = data
        },
        setHouseEstate (data) {
            this.housingEstate = data.houseEstate
            this.params.areaCode = data.addressOne
            this.fullAddress = data.fullAddress
        },
        // 身份证正面预览
        showIdCardFront () {
            let self = this
            let inputDOM = this.$refs.idcardFrontFile;
            // 通过DOM取文件数据
            this.params.idcardFrontFile = inputDOM.files[0];
            var reader = new FileReader();
            reader.onload = function (evt) {
                var srcString = evt.target.result;
                //安卓获取的base64数据无信息头，加之
                if (srcString.substring(5, 10) != "image") {
                    srcString = srcString.replace(/(.{5})/, "$1image/jpeg;");
                }
                self.idcardFront = srcString;
            }
            if (self.params.idcardFrontFile) {
                reader.readAsDataURL(self.params.idcardFrontFile);
            }
        },
        // 身份证正面预览
        showIdCardBack () {
            let self = this
            let inputDOM = this.$refs.idcardBackFile;
            // 通过DOM取文件数据
            this.params.idcardBackFile = inputDOM.files[0];
            var reader = new FileReader();
            reader.onload = function (evt) {
                var srcString = evt.target.result;
                //安卓获取的base64数据无信息头，加之
                if (srcString.substring(5, 10) != "image") {
                    srcString = srcString.replace(/(.{5})/, "$1image/jpeg;");
                }
                self.idcardBack = srcString;
            }
            if (self.params.idcardBackFile) {
                reader.readAsDataURL(self.params.idcardBackFile);
            }
        },
        // 验证数据是否合法
        checkParam() {
            let self = this
            let data = self.params
            for (let i in data) {
                if (!data[i]) {
                    self.toast = self.$createToast({
                        txt: '请填写所有项',
                        type: 'txt'
                    })
                    self.toast.show()
                    return false
                } else if (i === 'mobile') {
                    if (!this.checkMobile(data[i])) {
                        self.toast = self.$createToast({
                            txt: '请填写正确的手机号码！',
                            type: 'txt'
                        })
                        self.toast.show()
                        return false
                    }
                }
            }
            return true
        },
        // 提交按钮
        sumbit () {
            let self = this
            let data = self.params
            data.address = this.fullAddress + this.housingEstate + this.address
            var userId = getUrlParams().userId
            if (userId) {
                data.userId = userId
            }

            let param = new FormData()
            if (self.checkParam()) {
                for (let i in data) {
                    if (i === 'idcardFrontFile') {
                        param.append('file', data[i])
                    } else if (i === 'idcardBackFile') {
                        param.append('file', data[i])
                    } else {
                        param.append(i, data[i])
                    }                    
                }
            } else {
                return
            }
            if (!this.address) {
                self.toast = self.$createToast({
                    txt: '请输入您的详细住址！',
                    type: 'txt'
                })
                self.toast.show()
                return
            }
            self.btnDisabled = true
            Axios.post('promoter/promoter/inviteOrderMan.do', param).then(res => {
                if (res.respCode === 'S') {
                    const agreementBox = this.$refs.agreementBox
                    agreementBox.show()
                    this.$nextTick(() => {
                        this.$refs.scroll.refresh()
                    })
                } else {
                    this.toast = this.$createToast({
                        txt: res.errorMsg,
                        type: 'txt'
                    })
                    this.toast.show()
                    setTimeout(() => {
                        window.location.href = '../../index.html'
                    }, 2000)
                    self.btnDisabled = false
                }
            })
        },
        // 验证手机号码
        checkMobile(key) {
            const reg = /^((1[3-9][0-9])+\d{8})$/
            return reg.test(key)
        }
    }
})